'use strict';

const axios = require('axios');

const Controller = require('egg').Controller;

class WechatController extends Controller {
    async getAccessToken() {
        const { ctx, service } = this;

        try {
            const result = await service.wechat.getAccessTokenSrv();
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getNearbyPoiList() {
        const { ctx, service } = this;
        const { page, pageRows } = ctx.query;

        try {
            const accessToken = await service.wechat.getAccessTokenSrv();
            const result = await axios({
                url: 'https://api.weixin.qq.com/wxa/getnearbypoilist',
                method: 'get',
                params: {
                    page: 1,
                    page_rows: 20,
                    access_token: accessToken
                }
            }).then(resp => resp.data);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async sendCustomerMessage() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { openId } = body;

        try {
            const accessToken = await service.wechat.getAccessTokenSrv();
            const result = await axios({
                url: 'https://api.weixin.qq.com/cgi-bin/message/custom/send',
                method: 'post',
                params: {
                    access_token: accessToken
                },
                data: {
                    touser: 'o1Tnl5a0R29p4RHMj-j6eQgsWbTM',
                    msgtype: 'text',
                    text: {
                        content: 'hello world'
                    }
                }
            }).then(resp => resp.data);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async sendTemplateMessage() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { openId, templateId } = body;

        try {
            const accessToken = await service.wechat.getAccessTokenSrv();
            const result = await axios({
                url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send',
                method: 'post',
                params: {
                    access_token: accessToken
                },
                data: {
                    touser: 'o1Tnl5a0R29p4RHMj-j6eQgsWbTM',
                    template_id: 'YeUUB7ARFLbHd-_d8sMTeGB7UwnEkX3nYGq9vdL2U8E',
                    page: 'pages/tab-member',
                    data: {
                        keyword1: {
                            value: '新的活动'
                        },
                        keyword2: {
                            value: '去看海'
                        },
                        keyword3: {
                            value: '2018-10-24'
                        },
                        keyword4: {
                            value: 'chris'
                        }
                    }
                }
            }).then(resp => resp.data);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }
}

module.exports = WechatController;
